$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeper.html";
    });

    $("#goods_id").focus();

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "deliverBack.html";
    });

    //获取 仓库列表
    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        url: url + "/app/deliver/getProductWarehouses",
        type: "post",
        data: {},
        dataType: "json",
        json: "callback",
        cache: false,
        success: function (res) {
            if (res.success) {
                var data = res.result;
                //console.log(data);
                var data_html = '<option value="">请选择退货仓库</option>';
                for (var i = 0; i < data.length; i++) {
                    data_html += '<option value="' + data[i].warehouseCode + '">' + data[i].warehouseName + '</option>'
                }
                $("select.selectHouse").html(data_html);
            } else {
                $alert(res.message);
            }
        },
        error: function (error) {
            //console.log(error);
            $alert(error.responseText);
        }
    });
    $("select.selectHouse").on("change", function () {
        getSaleoutorders($(this).val(), $("#goods_id").val());
    });

    //扫描发货单号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == "13") {
            var code = $(this).val();
            if (!code) return $alert("单号不可为空");
            getSaleoutorders($("select.selectHouse").val(), code);
        }
    });

    //获取发货单列表
    getSaleoutorders("");

    function getSaleoutorders(house, code) {
        //清空页面中的发货单
        $("#work_list").find("li").remove();
        //加载提示符 显示
        $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            url: url + "/app/deliver/getSalebackorders",
            type: "post",
            data: {
                code: code,
                house: house
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    //清空页面中的发货单
                    $("#work_list").find("li").remove();
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    //console.log(data);
                    if (data.length == 0) return $alert("暂无要退货的运单");
                    var data_html = '';
                    for (var i = 0; i < data.length; i++) {
                        data_html += '<li>' +
                            '<p class="li_title">运单明细号：<span class="sp work_code">' + data[i].bill_Num + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                            '<div class="row no-gutter li_content"><div class="col-100">' +
                            '<p class="p_none"><span>id:</span><span class="sp goods_id">' + data[i]._id + '</span></p>' +
                            '<div class="details">' +
                            '<p><span>车牌:</span><span class="sp goods_plate_no">' + data[i].plate_Number + '</span>' +
                            '<span class="sp goods_driver_name">' + data[i].driver_Name + '</span></p>' +
                            '<p><span>收货仓库:</span><span class="sp goods_warehouseName">' + data[i].re_Ware_Name + '</span></p>' +
                            '<p><span>客户名称:</span><span class="sp goods_customerName">' + data[i].re_Company_Name + '</span></p>' +
                            '<p><span>物料编码:</span><span class="sp goods_productCode">' + data[i].inv_Code + '</span></p>' +
                            '<p><span>物料名称:</span><span class="sp goods_productName">' + data[i].inv_Name + '</span></p>' +
                            '<p><span>物料规格:</span><span class="sp goods_productSpecification">' + (data[i].inv_Spec || "") + '</span></p>' +
                            '<p><span>装货方式:</span><span class="sp goods_specialreq">' + data[i].specialreq + '</span></p>' +
                            '<p><span>件数:</span><span class="sp goods_planCount">' + (data[i].pre_Nums || "") + '</span>' +
                            '<span>数量:</span><span class="sp goods_planWeight">' + (data[i].pre_Weight || "") + '</span>T</p>' +
                            '<p><span>制单日期:</span><span class="sp goods_createDate">' + data[i].create_Date + '</span></p>';
                        if (data[i].comment) {
                            data_html += '<p><span>备注:</span><span class="sp comment">' + data[i].comment + '</span></p>';
                        }
                        data_html += '</div></div></div></li>';
                    }
                    $("#work_list").append(data_html);
                    //改变 共计 数
                    $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
                } else {
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    $alert(res.message);
                }
            },
            error: function (error) {
                //隐藏提示符 显示
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                if (error.status == 0) {
                    $alert("请求超时，请再试一次！");
                } else {
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    }

    //点击 发货单箭头
    $(".content #work_list").on("click", "li .li_title", function () {
        var id = $(this).parent().find("span.goods_id").html();
        var code = $(this).parent().find("span.work_code").html();
        var workplanNumber = $(this).parent().find("span.goods_workplanNumber").html();
        var productName = $(this).parent().find("span.goods_productName").html();
        var productSpecification = $(this).parent().find("span.goods_productSpecification").html();
        var planCount = $(this).parent().find("span.goods_planCount").html();
        var planWeight = $(this).parent().find("span.goods_planWeight").html();
        loading_show("选择中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + "/app/deliver/selectSalebackorder",
            data: {
                _id: id
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (data) {
                loading_hide();
                var work = {
                    locationInventorys: data,
                    id: id,
                    code: code,
                    productName: productName,
                    productSpecification: productSpecification,
                    planCount: planCount,
                    planWeight: planWeight
                };
                localStorage.setItem("work", JSON.stringify(work));
                window.location.href = "deliverBack_one.html";
            },
            error: function (error) {
                loading_hide();
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
    });

});